package com.ruoyi.web.controller.admin.acceptance;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.dto.AcceptanceChecklistTemplateCreateDTO;
import com.ruoyi.system.service.AcceptanceChecklistTemplateService;
import com.ruoyi.system.vo.AcceptanceChecklistTemplateVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 验收单模板管理控制器
 * 提供验收单模板的创建、查询、更新、删除等功能
 * 
 * @author ruoyi
 * @since 2024-01-01
 */
@RestController
@RequestMapping("/admin/acceptance/checklist/template")
//@PreAuthorize("@userType.isSys() && @ss.hasRole('admin')")
public class AcceptanceChecklistTemplateController extends BaseController {

    /**
     * 验收单模板服务
     */
    @Resource
    private AcceptanceChecklistTemplateService templateService;

    /**
     * 创建验收单模板
     * 
     * @param createDTO 创建参数
     * @return 模板ID
     */
    @PostMapping("/create")
    @Log(title = "验收单模板", businessType = BusinessType.INSERT)
    public AjaxResult create(@Valid @RequestBody AcceptanceChecklistTemplateCreateDTO createDTO) {
        Long templateId = templateService.createTemplate(createDTO);
        return AjaxResult.success(templateId);
    }

    /**
     * 更新验收单模板
     * 
     * @param templateId 模板ID
     * @param createDTO 更新参数
     * @return 更新结果
     */
    @PutMapping("/{templateId}")
    @Log(title = "验收单模板", businessType = BusinessType.UPDATE)
    public AjaxResult update(@PathVariable Long templateId, @Valid @RequestBody AcceptanceChecklistTemplateCreateDTO createDTO) {
        Boolean result = templateService.updateTemplate(templateId, createDTO);
        return result ? AjaxResult.success() : AjaxResult.error("更新失败");
    }

    /**
     * 删除验收单模板
     * 
     * @param templateId 模板ID
     * @return 删除结果
     */
    @DeleteMapping("/{templateId}")
    @Log(title = "验收单模板", businessType = BusinessType.DELETE)
    public AjaxResult delete(@PathVariable Long templateId) {
        Boolean result = templateService.deleteTemplate(templateId);
        return result ? AjaxResult.success() : AjaxResult.error("删除失败");
    }

    /**
     * 获取验收单模板详情
     * 
     * @param templateId 模板ID
     * @return 模板详情
     */
    @GetMapping("/{templateId}")
    public AjaxResult detail(@PathVariable Long templateId) {
        AcceptanceChecklistTemplateVO detail = templateService.getTemplateDetail(templateId);
        return AjaxResult.success(detail);
    }

    /**
     * 分页查询验收单模板
     * 
     * @param projectId 项目ID
     * @param stageId 阶段ID
     * @param templateName 模板名称
     * @param status 状态
     * @return 分页结果
     */
    @GetMapping("/page")
    public TableDataInfo page(@RequestParam(required = false) Long projectId,
                             @RequestParam(required = false) Long stageId,
                             @RequestParam(required = false) String templateName,
                             @RequestParam(required = false) Integer status) {
        PageDomain pageDomain = TableSupport.getPageDomain();
        Page<AcceptanceChecklistTemplateVO> result = templateService.pageTemplates(projectId, stageId, templateName, status, pageDomain.getPageNum(), pageDomain.getPageSize());
        TableDataInfo dataTable = getDataTable(result.getRecords());
        dataTable.setTotal(result.getTotal());
        return dataTable;
    }

    /**
     * 根据项目和阶段获取启用的验收单模板
     * 
     * @param projectId 项目ID
     * @param stageId 阶段ID
     * @return 模板列表
     */
    @GetMapping("/enabled")
    public AjaxResult getEnabledTemplates(@RequestParam Long projectId, @RequestParam Long stageId) {
        return AjaxResult.success(templateService.getEnabledTemplatesByProjectStage(projectId, stageId));
    }

    /**
     * 启用/禁用验收单模板
     * 
     * @param templateId 模板ID
     * @param status 状态
     * @return 操作结果
     */
    @PutMapping("/{templateId}/status")
    @Log(title = "验收单模板", businessType = BusinessType.UPDATE)
    public AjaxResult updateStatus(@PathVariable Long templateId, @RequestParam Integer status) {
        Boolean result = templateService.updateTemplateStatus(templateId, status);
        return result ? AjaxResult.success() : AjaxResult.error("状态更新失败");
    }
}
